import { Stock } from './entities/stock.entity';
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, Req } from '@nestjs/common';
import { StockService } from './stock.service';
import { CreateStockDto } from './dto/create-stock.dto';
import { UpdateStockDto } from './dto/update-stock.dto';
import { SearchDto } from './dto/search.dto';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { PublicRoute } from 'src/decorations/PublicRoute';
import { RedisServer } from 'src/config/Redis';
import Constant from 'src/config/Constant';
import * as dayjs from 'dayjs';
import Utils from 'src/utils/Utils';

@ApiTags('库存记录')
@Controller('stock')
export class StockController {
  constructor(
    private readonly stockService: StockService,
    private readonly redisServer: RedisServer,
  ) { }

  @ApiOperation({ summary: '入存/出库' })
  @Post('add')
  create(@Body() createStockDto: CreateStockDto, @Req() request: any) {
    createStockDto.creatorGuid = request?.userinfo?.guid || ''
    return this.stockService.create(createStockDto);
  }

  @ApiOperation({ summary: '分页记录' })
  @Post('list/page')
  findAllOut(@Body() searchDto: SearchDto) {
    return this.stockService.findAll(searchDto);
  }

  @ApiOperation({ summary: '入库记录' })
  @Post('list/put')
  findPutRecordByPId(@Body() searchDto: SearchDto) {
    return this.stockService.findAll(searchDto);
  }

  @ApiOperation({ summary: '出库记录' })
  @Post('list/record')
  findOutRecordByPId(@Body() searchDto: SearchDto) {
    return this.stockService.findAll(searchDto);
  }

  @ApiOperation({ summary: '出库/入库记录' })
  @Post('list/outAndPut')
  loadRecordByPId(@Body() createDto: CreateStockDto) {
    return this.stockService.loadRecordByPId(createDto);
  }

  /**
   * 设置当天过期
   * @returns
   */
  redisTodayOutDate() {
    const now = dayjs()
    const endOfDay = now.endOf('day')
    const diffInSeconds = endOfDay.diff(now, 'second');
    return diffInSeconds
  }

  @ApiOperation({ summary: '交易信息' })
  @Get('deal')
  async getDeal() {
    const redis = await this.redisServer.getClient();
    const key = `${Constant.redis_prefix}chart-data-deal`
    const result = await redis.get(key);
    if (result) return Utils.toJSON(result);

    const temp = await this.stockService.getDeal()
    redis.SETEX(key, this.redisTodayOutDate(), JSON.stringify(temp));
    return temp;
  }

  @ApiOperation({ summary: '首页柱状图' })
  @Get('home/bar/graph')
  async barGraph() {
    const redis = await this.redisServer.getClient();
    const key = `${Constant.redis_prefix}chart-data-bar-graph`
    const result = await redis.get(key);
    if (result) return Utils.toJSON(result);

    const temp = await this.stockService.barGraph()
    redis.SETEX(key, this.redisTodayOutDate(), JSON.stringify(temp));
    return temp;
  }

  @ApiOperation({ summary: '首页饼图' })
  @Get('home/nightingale')
  async nightingale() {
    const redis = await this.redisServer.getClient();
    const key = `${Constant.redis_prefix}chart-data-nightingale`
    const result = await redis.get(key);
    if (result) return Utils.toJSON(result);

    const temp = await this.stockService.nightingale()
    redis.SETEX(key, this.redisTodayOutDate(), JSON.stringify(temp));
    return temp;
  }

  @ApiOperation({ summary: '首页折线图' })
  @Get('home/chart/line')
  async chartLine() {
    const redis = await this.redisServer.getClient();
    const key = `${Constant.redis_prefix}chart-data-line`
    const result = await redis.get(key);
    if (result) return Utils.toJSON(result);

    const temp = await this.stockService.chartLine()
    redis.SETEX(key, this.redisTodayOutDate(), JSON.stringify(temp));
    return temp;
  }

  @ApiOperation({ summary: '入库分析' })
  @Get('home/product/put')
  async productPut() {
    const result = await this.stockService.productPut();
    return result
  }

  @ApiOperation({ summary: '出库分析' })
  @Get('home/product/out')
  async productOut() {
    return this.stockService.productOut();
  }


  @ApiOperation({ summary: '修改记录' })
  @Post('update/guid')
  updateStockByGuid(@Body() updateStockDto: UpdateStockDto) {
    return this.stockService.updateStock(updateStockDto);
  }


  @ApiOperation({ summary: '删除记录' })
  @Delete('delete/:guid')
  remove(@Param('guid') guid: string) {
    return this.stockService.remove(guid);
  }
}
